<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>以易于理解的格式打印变量。</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.print-r" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">print_r</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">print_r</span> &mdash; <span class="dc-title">
     以易于理解的格式打印变量。
    </span></p>

   </div>
   
 <div class="refsect1 description" id="refsect1-function.print-r-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>print_r</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$expression</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$return</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span>
  ] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>print_r()</strong></span> 以人类易读的格式显示一个变量的信息。
  </p>
  <p class="para">
   <span class="function"><strong>print_r()</strong></span>、
   <span class="function"><a href="var_dump.html" class="function">var_dump()</a></span>、
   <span class="function"><a href="var_export.html" class="function">var_export()</a></span> 
   都会显示对象 protected 和 private 的属性。
   Class 的静态属性（static） 则不会显示。
  </p>
 </div>

   
 <div class="refsect1 parameters" id="refsect1-function.print-r-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">expression</code></dt>

     <dd>

      <p class="para">
       要打印的表达式。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">return</code></dt>

     <dd>

      <p class="para">
       想要获取 <span class="function"><strong>print_r()</strong></span> 输出的内容，使用 <code class="parameter">return</code> 参数。
       当此参数为 <strong><code>TRUE</code></strong>，<span class="function"><strong>print_r()</strong></span> 会直接返回信息，而不是输出。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.print-r-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果输入的内容是 <span class="type"><a href="language.types.string.html" class="type string">string</a></span>、 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 或 <span class="type"><a href="language.types.float.html" class="type float">float</a></span>，会直接输出值本身。
   如果输入的内容是 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，展示的格式会显示数组的键和包含的元素。<span class="type"><a href="language.types.object.html" class="type object">object</a></span> 也类似。
  </p>
  <p class="para">
   当 <code class="parameter">return</code> 参数设置成 <strong><code>TRUE</code></strong>，本函数会返回 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 格式。否则返回 <strong><code>TRUE</code></strong>。
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.print-r-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">当使用了<code class="parameter">return</code>
参数时，本函数使用其内部输出缓冲，因此不能在
<span class="function"><a href="ob_start.html" class="function">ob_start()</a></span> 回调函数的内部使用。</p></p></blockquote>
 </div>


 <div class="refsect1 examples" id="refsect1-function.print-r-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6367">
    <p><strong>Example #1 <span class="function"><strong>print_r()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
&lt;pre&gt;<br /><span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'a'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'c'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;/pre&gt;</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;pre&gt;
Array
(
    [a] =&gt; apple
    [b] =&gt; banana
    [c] =&gt; Array
        (
            [0] =&gt; x
            [1] =&gt; y
            [2] =&gt; z
        )
)
&lt;/pre&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6368">
    <p><strong>Example #2 <code class="parameter">return</code> 参数的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$b&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'m'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'monkey'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$results&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$results&nbsp;包含了&nbsp;print_r&nbsp;的输出<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.print-r-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="ob_start.html" class="function" rel="rdfs-seeAlso">ob_start()</a> - 打开输出控制缓冲</span></li>
    <li class="member"><span class="function"><a href="var_dump.html" class="function" rel="rdfs-seeAlso">var_dump()</a> - 打印变量的相关信息</span></li>
    <li class="member"><span class="function"><a href="var_export.html" class="function" rel="rdfs-seeAlso">var_export()</a> - 输出或返回一个变量的字符串表示</span></li>
   </ul>
  </p>
 </div>

   
   
  </div></div></div></body></html>